#include "home.h"

extern esp_adc_cal_characteristics_t *adc_chars;

static home_app_t *local_app;

static lv_timer_t *adc_timer;
static lv_timer_t *read_temp_timer;
static lv_timer_t *update_time_timer;

extern lv_indev_t *my_indev;

extern app_t curr_page;
extern list_app_t list_app;

temperature_sensor_handle_t temp_handle;

LV_FONT_DECLARE(SourceHanSansSC_22);

void remove_home_app()
{
    lv_timer_del(adc_timer);
    lv_timer_del(read_temp_timer);
    lv_timer_del(update_time_timer);

    temperature_sensor_disable(temp_handle);
    temperature_sensor_uninstall(temp_handle);
}

static void adc_timer_callback(struct _lv_timer_t *arg)
{
    uint32_t adc_reading = 0;
    adc_reading = adc1_get_raw(ADC1_CHANNEL_8);
    uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);

    lv_label_set_text_fmt(local_app->label_adc, "   " LV_SYMBOL_BATTERY_FULL "%dmV", voltage * 2);
}

static void read_temp_timer_callback(struct _lv_timer_t *arg)
{
    float tsens_out;
    ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_handle, &tsens_out));

    lv_label_set_text_fmt(local_app->label_temp, "%.2f ℃", tsens_out);
}

static void update_time_timer_callback(struct _lv_timer_t *arg)
{
    uint32_t boot_time = esp_log_timestamp() / 1000;
    lv_label_set_text_fmt(local_app->label_runtime, "%02d:%02d", boot_time / 60, boot_time % 60);
}

static void my_event_cb(lv_event_t *event)
{
    if (event->code == LV_EVENT_CLICKED)
    {
        remove_home_app();
        create_list_app(&list_app);
    }
}

static void home_app_anim_in(lv_obj_t *obj)
{
    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, obj);
    lv_anim_set_time(&a, 300);
    lv_anim_set_delay(&a, 0);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_x);
    lv_anim_set_values(&a, lv_obj_get_x_aligned(obj) - 240, lv_obj_get_x_aligned(obj));
    lv_anim_start(&a);
}

void create_home_app(home_app_t *app)
{
    local_app = app;

    curr_page = HOME_APP;

    // 界面
    lv_obj_t *scr = lv_obj_create(NULL);
    lv_obj_set_width(scr, lv_pct(100));
    lv_obj_remove_style_all(scr);
    lv_obj_add_flag(scr, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_event_cb(scr, my_event_cb, LV_EVENT_CLICKED, NULL);

    // 顶部状态栏 ==============================================================
    lv_obj_t *line = lv_obj_create(scr);
    lv_obj_remove_style_all(line);
    lv_obj_clear_flag(line, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_width(line, lv_pct(100));
    lv_obj_set_height(line, LV_SIZE_CONTENT);
    lv_obj_set_style_bg_color(line, lv_color_hex(0x66D7D1), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(line, LV_OPA_100, LV_PART_MAIN);
    lv_obj_set_style_text_font(line, &lv_font_montserrat_14, LV_PART_MAIN);
    lv_obj_set_pos(line, 0, 0);

    local_app->label_timer = lv_label_create(line);
    lv_obj_set_style_align(local_app->label_timer, LV_ALIGN_LEFT_MID, LV_PART_MAIN);

    lv_obj_t *line_right = lv_obj_create(line);
    lv_obj_remove_style_all(line_right);
    lv_obj_set_style_align(line_right, LV_ALIGN_RIGHT_MID, LV_PART_MAIN);
    lv_obj_set_flex_flow(line_right, LV_FLEX_FLOW_ROW);
    lv_obj_set_height(line_right, LV_SIZE_CONTENT);
    lv_obj_set_width(line_right, 120);
    local_app->label_satellite_num = lv_label_create(line_right);
    local_app->label_adc = lv_label_create(line_right);

    lv_obj_fade_in(line, 300, 0);
    // ============================================================

    static lv_style_t style_all_line;
    lv_style_init(&style_all_line);
    lv_style_set_pad_left(&style_all_line, 20);
    lv_style_set_pad_right(&style_all_line, 20);
    lv_style_set_text_color(&style_all_line, lv_color_hex(0xF2EFEA));
    lv_style_set_width(&style_all_line, lv_pct(100));
    lv_style_set_height(&style_all_line, LV_SIZE_CONTENT);

    // 第一行=================================
    lv_obj_t *line_first = lv_obj_create(scr);
    lv_obj_remove_style_all(line_first);
    lv_obj_set_pos(line_first, 0, 25);
    lv_obj_add_style(line_first, &style_all_line, LV_PART_MAIN);

    // 第一行样式
    static lv_style_t style_first_line;
    lv_style_init(&style_first_line);
    lv_style_set_bg_color(&style_first_line, lv_color_hex(0x403D58));
    lv_style_set_bg_opa(&style_first_line, LV_OPA_100);
    lv_style_set_width(&style_first_line, 128);
    lv_style_set_height(&style_first_line, 80);
    lv_style_set_pad_all(&style_first_line, 8);
    lv_style_set_radius(&style_first_line, 5);
    lv_style_set_shadow_width(&style_first_line, 10);                     //设置样式阴影宽度
    lv_style_set_shadow_color(&style_first_line, lv_color_make(0, 0, 0)); //设置样式阴影颜色
    lv_style_set_shadow_opa(&style_first_line, 80);

    // 速度
    lv_obj_t *line_first_speed = lv_obj_create(line_first);
    lv_obj_remove_style_all(line_first_speed);
    lv_obj_add_style(line_first_speed, &style_first_line, LV_PART_MAIN);
    lv_obj_set_align(line_first_speed, LV_ALIGN_LEFT_MID);
    lv_obj_set_flex_flow(line_first_speed, LV_FLEX_FLOW_COLUMN);

    // 高度
    lv_obj_t *line_first_alt = lv_obj_create(line_first);
    lv_obj_remove_style_all(line_first_alt);
    lv_obj_add_style(line_first_alt, &style_first_line, LV_PART_MAIN);
    lv_obj_set_align(line_first_alt, LV_ALIGN_RIGHT_MID);
    lv_obj_set_flex_flow(line_first_alt, LV_FLEX_FLOW_COLUMN);

    // 内容
    lv_obj_t *label_speed_text = lv_label_create(line_first_speed);
    lv_label_set_text_static(label_speed_text, "速度/航向");
    lv_obj_set_style_text_font(label_speed_text, &SourceHanSansSC_22, LV_PART_MAIN);
    lv_obj_t *label_alt_text = lv_label_create(line_first_alt);
    lv_label_set_text_static(label_alt_text, "海拔/半球");
    lv_obj_set_style_text_font(label_alt_text, &SourceHanSansSC_22, LV_PART_MAIN);

    local_app->label_speed = lv_label_create(line_first_speed);
    local_app->label_alt = lv_label_create(line_first_alt);
    local_app->label_ne = lv_label_create(line_first_alt);

    home_app_anim_in(line_first);
    // ============================================
    // 第二行 ==============================================
    lv_obj_t *line_sec = lv_obj_create(scr);
    lv_obj_remove_style_all(line_sec);
    lv_obj_set_pos(line_sec, 0, 113);
    lv_obj_add_style(line_sec, &style_all_line, LV_PART_MAIN);

    // 坐标
    lv_obj_t *line_sec_coord = lv_obj_create(line_sec);
    lv_obj_remove_style_all(line_sec_coord);
    lv_obj_add_style(line_sec_coord, &style_first_line, LV_PART_MAIN);
    lv_obj_set_style_width(line_sec_coord, lv_pct(100), LV_PART_MAIN);
    lv_obj_set_style_height(line_sec_coord, 30, LV_PART_MAIN);

    local_app->label_coordinate = lv_label_create(line_sec_coord);
    lv_obj_set_align(local_app->label_coordinate, LV_ALIGN_CENTER);

    home_app_anim_in(line_sec);
    // ========================================================
    // 第三行 ==============================================
    lv_obj_t *line_third = lv_obj_create(scr);
    lv_obj_remove_style_all(line_third);
    lv_obj_set_pos(line_third, 0, 151);
    lv_obj_add_style(line_third, &style_all_line, LV_PART_MAIN);

    static lv_style_t style_line_small;
    lv_style_init(&style_line_small);
    lv_style_set_width(&style_line_small, 128);
    lv_style_set_height(&style_line_small, 30);

    // 运行时间
    lv_obj_t *line_third_left = lv_obj_create(line_third);
    lv_obj_remove_style_all(line_third_left);
    lv_obj_add_style(line_third_left, &style_first_line, LV_PART_MAIN);
    lv_obj_add_style(line_third_left, &style_line_small, LV_PART_MAIN);
    // lv_obj_set_style_width(line_third_left, 128, LV_PART_MAIN);
    // lv_obj_set_style_height(line_third_left, 30, LV_PART_MAIN);
    lv_obj_set_style_align(line_third_left, LV_ALIGN_LEFT_MID, LV_PART_MAIN);

    local_app->label_runtime = lv_label_create(line_third_left);
    lv_obj_set_align(local_app->label_runtime, LV_ALIGN_CENTER);

    // 定位指示
    lv_obj_t *line_third_right = lv_obj_create(line_third);
    lv_obj_remove_style_all(line_third_right);
    lv_obj_add_style(line_third_right, &style_first_line, LV_PART_MAIN);
    lv_obj_add_style(line_third_right, &style_line_small, LV_PART_MAIN);
    lv_obj_set_style_align(line_third_right, LV_ALIGN_RIGHT_MID, LV_PART_MAIN);

    local_app->label_gps_status = lv_label_create(line_third_right);
    lv_obj_set_align(local_app->label_gps_status, LV_ALIGN_CENTER);

    home_app_anim_in(line_third);
    // 第四行 ==============================================
    lv_obj_t *line_four = lv_obj_create(scr);
    lv_obj_remove_style_all(line_four);
    lv_obj_set_pos(line_four, 0, 189);
    lv_obj_add_style(line_four, &style_all_line, LV_PART_MAIN);

    // 测试
    lv_obj_t *line_four_left = lv_obj_create(line_four);
    lv_obj_remove_style_all(line_four_left);
    lv_obj_add_style(line_four_left, &style_first_line, LV_PART_MAIN);
    lv_obj_add_style(line_four_left, &style_line_small, LV_PART_MAIN);
    lv_obj_set_style_align(line_four_left, LV_ALIGN_LEFT_MID, LV_PART_MAIN);
    lv_obj_set_height(line_four_left, 40);

    // 测试
    lv_obj_t *line_four_right = lv_obj_create(line_four);
    lv_obj_remove_style_all(line_four_right);
    lv_obj_add_style(line_four_right, &style_first_line, LV_PART_MAIN);
    lv_obj_add_style(line_four_right, &style_line_small, LV_PART_MAIN);
    lv_obj_set_style_align(line_four_right, LV_ALIGN_RIGHT_MID, LV_PART_MAIN);
    lv_obj_set_height(line_four_right, 40);

    local_app->label_direction = lv_label_create(line_four_left);
    lv_obj_set_style_text_font(local_app->label_direction, &SourceHanSansSC_22, LV_PART_MAIN);
    lv_obj_set_align(local_app->label_direction, LV_ALIGN_CENTER);

    local_app->label_temp = lv_label_create(line_four_right);
    lv_obj_set_style_text_font(local_app->label_temp, &SourceHanSansSC_22, LV_PART_MAIN);
    lv_obj_set_align(local_app->label_temp, LV_ALIGN_CENTER);

    home_app_anim_in(line_four);

    lv_group_t *g = lv_group_create();
    lv_group_add_obj(g, scr);
    lv_indev_set_group(my_indev, g);

    lv_obj_del(lv_scr_act());
    lv_scr_load(scr);
    // lv_scr_load_anim(scr, LV_SCR_LOAD_ANIM_MOVE_TOP, 300, 0, true);

    temperature_sensor_config_t temp_sensor = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
    temp_sensor.clk_src = SOC_MOD_CLK_RC_FAST;
    ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor, &temp_handle));
    ESP_ERROR_CHECK(temperature_sensor_enable(temp_handle));

    adc_timer = lv_timer_create(adc_timer_callback, 5000, NULL);
    lv_timer_ready(adc_timer);

    read_temp_timer = lv_timer_create(read_temp_timer_callback, 10000, NULL);
    lv_timer_ready(read_temp_timer);

    update_time_timer = lv_timer_create(update_time_timer_callback, 1000, NULL);
    lv_timer_ready(update_time_timer);
}
